package cn.getech.data.metadata.model.excel;

import cn.getech.data.metadata.annotation.ExplicitConstraint;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
public class CollectionBloodExcel {

    @ExcelProperty(value = {"*source的数据源ip"}, order = 1)
    @NotBlank(message = "source的数据源ip不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含数字和.")
    @ColumnWidth(26)
    private String sourceIp;

    @ExcelProperty(value = {"*source端口"}, order = 2)
    @NotNull(message = "source端口不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含数字")
    @ColumnWidth(26)
    private Integer sourcePort;

    @ExcelProperty(value = {"*source数据库名称"}, order = 3)
    @NotBlank(message = "source数据库名称不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    private String sourceName;


    @ExcelProperty(value = "*source表名称",order =4)
    @NotBlank(message = "source表不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    private String sourceTableName;


    @ExcelProperty(value = "*source字段名称",order =5)
    @NotBlank(message = "source字段名称不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    private String sourceFiledName;

    @ExcelProperty(value = {"*target的数据源ip"}, order = 10)
    @NotBlank(message = "target的数据源ip不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含数字和.")
    @ColumnWidth(26)
    protected String targetIp;


    @ExcelProperty(value = {"*target端口"}, order = 20)
    @NotNull(message = "target端口不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含数字")
    @ColumnWidth(26)
    protected Integer targetPort;

    @ExcelProperty(value = {"*target数据库名称"}, order = 30)
    @NotBlank(message = "target数据库名称不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    protected String targetName;


    @ExcelProperty(value = "*target表名称",order =40)
    @NotBlank(message = "target表不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    private String targetTableName;

    @ExcelProperty(value = "*target字段名称",order =50)
    @NotBlank(message = "target字段名称不能为空")
    @HeadFontStyle(color = 0)//red
    @ExplicitConstraint(desc = "必填项，值只能包含字母,数字,下划线")
    @ColumnWidth(26)
    private String targetFiledName;


    public String getUniqueBlood() {
        return sourceIp + sourcePort + sourceName + sourceTableName + sourceFiledName
                + targetIp + targetPort + targetName + targetTableName + targetFiledName;
    }

    private boolean checkSelf(){
        return (sourceIp + sourcePort + sourceName + sourceTableName)
                .equals((targetIp + targetPort + targetName + targetTableName));
    }

    public String validateValue() {
        String res = "";
        if (sourcePort != null && sourcePort < 0) {
            res = res + "端口必须大于0";
        }
        if (targetPort != null && targetPort < 0) {
            res = res + "端口必须大于0";
        }
        if (targetPort != null && targetPort < 0) {
            res = res + "端口必须大于0";
        }
        if (checkSelf()) {
            res = res + "血缘中表["+sourceTableName+"]不能自我关联";
        }
        return res;
    }

    /**
     * 保存数据顺序
     */
    @ExcelIgnore
    private Integer order=1;

    private static final long serialVersionUID = 1L;

}